<?php

namespace app\services;

use app\model\AuthUsers;

/**
 * 缓存服务模块
 *
 * @package App\Services
 */
class CacheService
{
    protected $handler;

    protected $params = [];

    //DB库
    const CONFIG_DB = 0;//全局配置库
    const GOODS_INFO_DB = 1;//商品信息库
    const USER_INFO_DB = 2;//用户信息库

    /**
     * Redis constructor
     * 缓存库说明-连接阿里云RDS库有0-255个库
     * 暂定分库说明：
     * 0 - 全局配置类库
     * 1 - 商品信息
     * 2 - C端用户信息
     */
    public function __construct()
    {
        $this->params = config('cache.stores.redis');
    }

    /**
     * 初始化redis
     */
    private function initRedis()
    {
        $this->handler = app('think\cache\driver\Redis', [$this->params]);
    }

    /**
     * 存储当前登录的用户信息
     *
     * @param string $token
     * @param array $userInfo
     * @return mixed
     */
    public function setUserInfo(string $token, array $userInfo)
    {return false;
        $this->params['select'] = self::USER_INFO_DB;
        $this->initRedis();
        return $this->handler->set($token, $userInfo, 3600 * 24);
    }

    /**
     * 获取当前登录的用户信息
     *
     * @param string $token
     * @return mixed
     */
    public function getUserInfo(string $token)
    {
        return (new AuthUsers())->getInfoByToken($token);
        $this->params['select'] = self::USER_INFO_DB;
        $this->initRedis();
        return $this->handler->get($token);
    }

    /**
     * 获取当前登录的用户信息
     *
     * @param string $token
     * @return mixed
     */
    public function deleteUserInfo(string $token)
    {return false;
        $this->params['select'] = self::USER_INFO_DB;
        $this->initRedis();
        return $this->handler->delete($token);
    }

    /**
     * 设置微信的accessToken
     *
     * @return mixed
     */
    public function setAccessToken()
    {return false;
        $this->params['select'] = 1;
        $this->initRedis();
        return $this->handler->set('access_token', time() . 'aa', 3600);
    }


    /**
     * 设置区域列表
     *
     * @param array $data
     * @return mixed
     */
    public function setRegionList(array $data)
    {
        return false;
        $this->params['select'] = self::CONFIG_DB;
        $this->initRedis();
        return $this->handler->set('region_list', $data);
    }

    /**
     * 获取区域列表
     *
     * @return mixed
     */
    public function getRegionList()
    {
        return false;
        $this->params['select'] = self::CONFIG_DB;
        $this->initRedis();
        return $this->handler->get('region_list');
    }

    /**
     * 获取微信的accessToken
     *
     * @return mixed
     */
    public function getAccessToken()
    {return false;
        $this->params['select'] = 1;
        $this->initRedis();
        return $this->handler->get('access_token');
    }

    /**
     * 设置平台银行列表
     *
     * @param array $bankList
     * @return mixed
     */
    public function setBankList(array $bankList)
    {return false;
        $this->params['select'] = self::CONFIG_DB;
        $this->initRedis();
        return $this->handler->set('bank_list', $bankList);
    }

    /**
     * 获取平台银行列表
     *
     * @return mixed
     */
    public function getBankList()
    {return false;
        $this->params['select'] = self::CONFIG_DB;
        $this->initRedis();
        return $this->handler->get('bank_list');
    }
}